﻿namespace BeatEmUp.World
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using BeatEmUp.Menu;
    using BeatEmUp.Characters.Enemies;
    using BeatEmUp.HUD;
    using BeatEmUp.Items.Weapons;
    using BeatEmUp.Enums;
    using Microsoft.Xna.Framework.Content;
    using BeatEmUp.Characters;

    [Serializable]
    public class Screen
    {
        protected GameManager gameManager;
        protected Viewport viewport;
        protected Drawable.IDrawable background;
        protected Config<Element> backgroundConfig;
        protected Config<StaticElement> staticElementsConfig;

        public Screen(GameManager gameManager, Drawable.IDrawable background, Config<Element> backgroundConfig, Config<StaticElement> staticElementsConfig, int WorldEndingAt)
        {
            this.gameManager = gameManager;
            this.background = background;
            this.viewport = new Viewport(gameManager.WorldWidth, WorldEndingAt);
            this.backgroundConfig = backgroundConfig;
            this.staticElementsConfig = staticElementsConfig;
        }

        public void LoadContent(ContentManager content)
        {
            this.background.LoadContent(content);
            this.backgroundConfig.LoadContent(content);
            this.staticElementsConfig.LoadContent(content);

            this.backgroundConfig.DesignateCurrentElements(this.viewport, Direction.Right);
            this.staticElementsConfig.DesignateCurrentElements(this.viewport, Direction.Right);

            CollisionManager.CreateListFromConfig(this.staticElementsConfig, this.viewport);
        }

        public void Update(GameTime gameTime)
        {
            this.background.Update(gameTime);

            foreach (Element element in this.backgroundConfig.CurrentElements)
            {
                element.Update(gameTime);
            }

            foreach (Element element in this.staticElementsConfig.CurrentElements)
            {
                element.Update(gameTime);
            }
        }

        public void Draw(GameTime gameTime)
        {
            this.background.Draw(this.gameManager, gameTime, new Vector2(0, this.gameManager.HudHeight / GameManager.customResolution.ScaleRatio()));

            foreach (Element element in this.backgroundConfig.CurrentElements)
            {
                element.Draw(this.gameManager, gameTime, this.viewport.WorldPosition);
            }

            foreach (Element element in this.staticElementsConfig.CurrentElements)
            {
                element.Draw(this.gameManager, gameTime, this.viewport.WorldPosition);
            }
        }

        public void MoveTo(int newPosition)
        {
            Direction direction = (this.viewport.WorldPosition > newPosition) ? Direction.Left : Direction.Right;
            this.viewport.WorldPosition = newPosition;

            this.backgroundConfig.DesignateCurrentElements(this.viewport, direction);
            this.staticElementsConfig.DesignateCurrentElements(this.viewport, direction);

            CollisionManager.CreateListFromConfig(this.staticElementsConfig, this.viewport);
        }

        public void MoveBy(int by, Direction direction = Direction.Right)
        {
            int newPosition = this.viewport.WorldPosition + (int)direction * by;
            this.MoveTo(newPosition);
        }

        public bool CanScroll()
        {
            return !this.viewport.WorldEnded();
        }

        public int worldPos()
        {
            return viewport.WorldPosition;
        }
    }
}
